<?PHP
	class Article {

		function index(){

			$article=D("article");
			$column=D("column");
			$time=strtotime(date("Y-m-d"))+60*60*24;
			//获取并分配推荐文章
			$this->assign("recommends", $article->field('id,title,recommend')->where(array("recommend >"=>0, "audit"=>1))->order("recommend desc")->limit(10)->select());
			//获取并分配最新更新文章
			$data = $article->field('id,title,summary,posttime,views,picid')->where(array("audit"=>1))->order("posttime desc")->limit(6)
				            ->r_select( array("image", 'name as imgname', 'id', 'picid'));
			$this->assign("news", $data);
			//获取并分配热门文章
			$this->assign("hots", $article->field('id,title,posttime,views,picid')->where(array("posttime >"=>($time-60*60*24*30), "audit"=>1))->order("views desc")->limit(10)
				 ->r_select( array("image", 'name as imgname', 'id', 'picid')));
	
			//获取并分配所有栏目 
			$col=$column->field("id,title,picid")->order("ord asc")->where(array("pid"=>0,"display"=>1))
				        ->select();

			$this->assign("cols", $col);

			//p($data);
			//获取并分配友情链接内容
			$this->assign("links", D("flink")->field('id, webname,url')
				 ->where(array("audit"=>1))->order("ord asc")->select());


			//文章分页
			if(!empty($_GET["pid"])){
				$where["pid"]=$_GET["pid"];
				$pget.="/pid/{$_GET["pid"]}";
				$pid=$_GET['pid'];
			}else{
				$pid=0;
			}

			$page=new Page($article->total($where), ARTICLE_PAGE_SIZE,$pget);

			$arts = $article->field('id,title,summary,posttime,views,picid')
							->where(array("audit"=>1))
							->order('id desc')
				            ->limit($page->limit)
				            ->r_select( array("image", 'name as imgname', 'id', 'picid'));

			$this->assign("arts", $arts);
			$this->assign("fpage", $page->fpage());

			$this->display();


		}

		function Details(){

			$article=D("article");
			$column=D("column");
	
			//获取并分配所有栏目 
			$col=$column->field("id,title,picid")->order("ord asc")->where(array("pid"=>0,"display"=>1))
				        ->select();

			$this->assign("cols", $col);

			//p($data);
			//获取并分配友情链接内容
			$this->assign("links", D("flink")->field('id, webname,url')
				 ->where(array("audit"=>1))->order("ord asc")->select());

			$data=$article->where(array("id"=>$_GET["aid"]))->find();
			$data["content"]=str_replace(array("&quot;", "&#039;"),array("\"", "'"), $data["content"]);

			$data["author"]="杨永贞";
			//将文章分配到模板中
			$this->assign("article", $data);

			$columns=$column->find($data["pid"]);
			//获取并分配将前位置到模板中
			$locs=$column->field('id,title,description')->where(str_replace('-',',', $columns["path"]).','.$columns["id"])->select();

			$this->assign("locs", $locs);

			$prevwhere=array("audit"=>1, "pid"=>$data["pid"],"id <"=>$_GET["aid"]);
			$nextwhere=array("audit"=>1, "pid"=>$data["pid"],"id >"=>$_GET["aid"]);
			$connwhere=array("audit"=>1, "pid"=>$data["pid"]);
			$recowhere=array("audit"=>1, "pid"=>$data["pid"], "recommend >"=>0);
			$hotswhere=array("audit"=>1, "pid"=>$data["pid"]);

			//获取和当前文章在同一个类别中的上一篇审核过的文章	
			$this->assign("prevArticle",$article->field("id, title")->where($prevwhere)->order("id DESC")->find());
	
			//获取和当前文章在同一个类别中的下一篇审核过的文章
			$this->assign("nextArticle", $article->field("id, title")->where($nextwhere)->order("id ASC")->find());

			//获取同类中的相关文章
			$data = $article->field("id, title,posttime,picid")
				            ->where($connwhere)->order("id DESC")->limit(10)
				            ->r_select( array("image", 'name as imgname', 'id', 'picid'));
			//p($data);	            
			$this->assign("conns", $data);

			$article->where(array("id"=>$_GET["aid"]))->update("views=views+1");
			
			$this->display();
			
		}


		function lists()
		{
			$column=D("column");
			$article=D("article");
			$time=strtotime(date("Y-m-d"))+60*60*24;
			
			$current=$column->where(array("id"=>$_GET["pid"], "display"=>1))->find();
	
			if(!$current){
				$this->error("该栏目不存在或已经被关闭!", 3, "index/index");
			}

			$locs=$column->field('id,title,description')->where(str_replace('-',',', $current["path"]).','.$_GET["pid"])->select();
			$this->assign("locs",$locs);

			//获取并分配所有栏目 
			$col=$column->field("id,title,picid")->order("ord asc")->where(array("pid"=>0,"display"=>1))
				        ->select();

			$this->assign("cols", $col);

			$wherelist=array("pid"=>$_GET["pid"], "audit"=>1);

			$page=new Page($article->where($wherelist)->total(), ARTICLE_PAGE_SIZE, "pid/{$_GET["pid"]}");
			$this->assign("arts", $article->field('id,title,summary,posttime,views,picid')
				 ->where($wherelist)->order("id desc")
				 ->limit($page->limit)
				 ->r_select(array("image", 'name as imgname', 'id', 'picid')));

			$this->assign("fpage", $page->fpage(0,3,4,5,6));

			
			//获取并分配推荐文章
			$this->assign("recommends", $article->field('id,title,recommend')->where(array("recommend >"=>0, "audit"=>1))->order("recommend desc")->limit(10)->select());
			//获取并分配最新更新文章
			$data = $article->field('id,title,summary,posttime,views,picid')->where(array("audit"=>1))->order("posttime desc")->limit(6)
				            ->r_select( array("image", 'name as imgname', 'id', 'picid'));
			$this->assign("news", $data);
			//获取并分配热门文章
			$this->assign("hots", $article->field('id,title,posttime,views,picid')->where(array("posttime >"=>($time-60*60*24*30), "audit"=>1))->order("views desc")->limit(10)
				 ->r_select( array("image", 'name as imgname', 'id', 'picid')));
			//获取并分配友情链接内容
			$this->assign("links", D("flink")->field('id, webname,url')
				 ->where(array("audit"=>1))->order("ord asc")->select());

			$this->display();
		}

	}